package com.digitalxpert.jargle.TestGui;
import java.awt.Component;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JTable;
import javax.swing.TransferHandler;
public class TablePopupMenu extends JPopupMenu {
	private Action copyA;
	private Action pasteA;
	private Action selectAllA;
	private Action cutA;
	private JTable table;
	private JMenuItem copyItem;
	private JMenuItem pasteItem;
	private JMenuItem selectAllItem;
	private JMenuItem cutItem;
	public TablePopupMenu() {
		copyA = new CopyAction();
		pasteA = new PasteAction();
		selectAllA = new SelectAllAction();
		cutA = new CutAction();
		copyItem = new JMenuItem(copyA);
		pasteItem = new JMenuItem(pasteA);
		selectAllItem = new JMenuItem(selectAllA);
		cutItem = new JMenuItem(cutA);
		this.add(copyItem);
		// this.add(pasteItem);
		this.add(selectAllItem);
		// this.add(cutItem);
	}
	public void show(Component invoker, int x, int y) {
		super.show(invoker, x, y);
		table = (JTable) invoker;
		setModifiable(table);
	}
	public void setModifiable(JTable component) {
		
		/*
		 * canModify = table.is; String sel = textComp.getSelectedText(); if
		 * (canModify) { pasteItem.setEnabled(true); cutItem.setEnabled(true); }
		 * else { pasteItem.setEnabled(false); cutItem.setEnabled(false); }
		 * copyItem.setEnabled(sel != null && sel.length() > 0);
		 */
	}
	public class CopyAction extends AbstractAction {
		public CopyAction() {
			super("Copy");
			putValue(SHORT_DESCRIPTION, "Copy2");
		}
		public void actionPerformed(ActionEvent e) {
			copy();
		}
		private void copy() {
			StringBuffer selection = new StringBuffer();
			int numcols = table.getColumnCount();
			int numrows = table.getSelectedRowCount();
			/*int[] rowsselected = table.getSelectedRows();
			int[] colsselected = table.getSelectedColumns();*/
			for (int i = 0; i < numrows; i++) {
				for (int j = 0; j < numcols; j++) {
					selection.append(table.getValueAt(i, j));
					if (j < numcols - 1)
						selection.append("\t");
				}
				selection.append("\n");
			}
			StringSelection data = new StringSelection(selection.toString());
			//TransferHandler th = table.getTransferHandler();
			Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
			//th.exportToClipboard(table, clipboard, TransferHandler.COPY);
			clipboard.setContents(data, data);
		}
	}
	public class PasteAction extends AbstractAction {
		public PasteAction() {
			super("Paste");
			putValue(SHORT_DESCRIPTION, "Paste2");
		}
		public void actionPerformed(ActionEvent e) {
			/*
			 * Clipboard clipboard =
			 * Toolkit.getDefaultToolkit().getSystemClipboard(); Transferable
			 * clipData = clipboard.getContents(clipboard); if (clipData !=
			 * null) { if
			 * (clipData.isDataFlavorSupported(DataFlavor.stringFlavor)) {
			 * String s = ""; try { s = (String)
			 * (clipData.getTransferData(DataFlavor.stringFlavor));
			 * textComp.setText(s); } catch (UnsupportedFlavorException e1) {
			 * e1.printStackTrace(); } catch (IOException e1) {
			 * e1.printStackTrace(); } } }
			 */
		}
	}
	public class SelectAllAction extends AbstractAction {
		public SelectAllAction() {
			super("SelectAll");
			putValue(SHORT_DESCRIPTION, "SelectAll2");
		}
		public void actionPerformed(ActionEvent e) {
			table.selectAll();
		}
	}
	public class CutAction extends AbstractAction {
		public CutAction() {
			super("Cut");
			putValue(SHORT_DESCRIPTION, "Cut");
		}
		public void actionPerformed(ActionEvent e) {
		}
	}
}
